/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonInfrastructure.accountManagement.managedbean;

import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import commonInfrastructure.accountManagement.session.PasswordHashSessionBean;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class ValidatePasswordManagedBean {

    @EJB
    private EmployeeSessionBean employeeManagerSessionBean;
    @EJB
    private PasswordHashSessionBean passwordHashSessionBean;
    private EmployeeEntity employee;
    private String passwordTemp;

    public ValidatePasswordManagedBean() {
        passwordHashSessionBean = new PasswordHashSessionBean();
    }

    public void initView(PhaseEvent event) {
        if (employee == null) {
            employee = (EmployeeEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("employee");
        }

    }

    public void doSubmit(ActionEvent event) throws IOException {

        if (employee.getPassword().equals(passwordHashSessionBean.hashPassword(employee.getUserName()+passwordTemp))) {
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("employee",employee);
            FacesContext.getCurrentInstance().getExternalContext().redirect("changePassword.xhtml");

        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Invalid Password!", ""));

        }
    }

    /**
     * @return the passwordTemp
     */
    public String getPasswordTemp() {
        return passwordTemp;
    }

    /**
     * @param passwordTemp the passwordTemp to set
     */
    public void setPasswordTemp(String passwordTemp) {
        this.passwordTemp = passwordTemp;
    }

    

}
